﻿using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;

namespace Dsl.Ped.Common.Converters
{
    /// <summary>
    /// Convert a <see cref="ICollection"/> to its size. 
    /// </summary>
    public class CollectionToCountConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// Convert a <see cref="ICollection"/> to its size
        /// </summary>
        /// <returns>Size of a collection, 0 if collection is null</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return 0;

            // If collection is not null, return its size
            if (value is ICollection) return (value as ICollection).Count;

            throw new ArgumentException("Passed argument does not implement ICollection interface!");
        }

        /// <summary>
        /// Backwards convertibility is not supported, <see cref="NotImplementedException"/> is thrown
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
